package monteCarlo;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseAdapter;
import java.util.Arrays;
import java.util.LinkedList;

public class AlgoVisualizer {

    private static int DELAY = 40;
    private MonteCarloPiData monteCarloPiData;
    private AlgoFrame frame;    // 视图
    /**  打点数 */
    private Integer N;


    public AlgoVisualizer(int sceneWidth, int sceneHeight, Integer N){
        if (sceneWidth!=sceneHeight) throw new IllegalArgumentException("This demo must be run in al square!!!");
        // 初始化数据
        Circle circle = new Circle(sceneWidth / 2, sceneHeight / 2, sceneWidth / 2);
        monteCarloPiData = new MonteCarloPiData(circle);
        this.N = N;
        // 初始化视图
        EventQueue.invokeLater(() -> {
            frame = new AlgoFrame("Welcome", sceneWidth, sceneHeight);
            new Thread(() -> run()).start();
        });
    }

    // 动画逻辑
    private void run(){
        for (Integer i = 0; i < N; i++) {
            if (i % 100 == 0) {
                frame.render(monteCarloPiData);
                AlgoVisHelper.pause(DELAY);
                System.out.println(monteCarloPiData.estimatePi());
            }
            int x = (int) (Math.random() * frame.getCanvasWidth());
            int y = (int) (Math.random() * frame.getCanvasHeight());
            monteCarloPiData.addPoint(new Point(x, y));
        }
    }

    // TODO: 根据情况决定是否实现键盘鼠标等交互事件监听器类
    private class AlgoKeyListener extends KeyAdapter{ }
    private class AlgoMouseListener extends MouseAdapter{ }

    public static void main(String[] args) {

        int sceneWidth = 800;
        int sceneHeight = 800;
        int N = 50000;

        AlgoVisualizer visualizer = new AlgoVisualizer(sceneWidth, sceneHeight, N);
    }
}
